package org.douxiaoban.client.widget;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.douxiaoban.client.R;
import org.douxiaoban.client.activity.CollectionBookListActivity;
import org.douxiaoban.client.activity.CollectionMovieListActivity;
import org.douxiaoban.client.activity.CollectionMusicListActivity;
import org.douxiaoban.client.activity.DouMyGroupAct;
import org.douxiaoban.client.activity.MailActivity;
import org.douxiaoban.client.activity.MiniBlogActivity;
import org.douxiaoban.client.activity.UserHomeActivity;
import org.douxiaoban.client.app.App;
import org.douxiaoban.client.app.Assistant;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by IntelliJ IDEA.
 * User: huangzhongkun
 * Date: 11-6-10
 * Time: 下午6:23
 */
public class TitleStyle {
    private Activity _activity;
    private LayoutInflater _infalter;
    private static SimpleDateFormat format ;
    private RelativeLayout _rl;
    private ImageView _title_douban_icon_image;
    private ImageView _title_sun_icon_image;
    private ImageButton _title_avatar_btn;
    private TextView _title_tx;

    private int icon_view_id = 1384849;
    private int sun_view_id = 2349384;

    private PopupWindow _user_panel_windows;
    private View _user_panel_view;

    private int _screen_count;
    private int _current_screen;
    private ArrayList<ImageView> _control_images = new ArrayList<ImageView>();
    private LinearLayout _page_control_lay;
    
  

    public TitleStyle(final Activity activity){
        _activity = activity;
        _infalter = LayoutInflater.from(activity);
        _title_sun_icon_image = (ImageView)activity.findViewById(R.id.title_sun_icon_image_1);
        _rl = (RelativeLayout)activity.findViewById(R.id.title_relative_lay);
        _title_avatar_btn = (ImageButton)activity.findViewById(R.id.title_avatar_btn);
        _page_control_lay = (LinearLayout)activity.findViewById(R.id.page_controll_lay);
        _title_tx = (TextView)activity.findViewById(R.id.title_tx);

        _title_avatar_btn.setOnClickListener(avatarClickListener);

        _user_panel_view = _infalter.inflate(R.layout.user_selection_dialog,null,true);

        //获取屏幕宽度
        DisplayMetrics dm = _activity.getResources().getDisplayMetrics();

        if(activity.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_PORTRAIT){
            _user_panel_windows = new PopupWindow(_user_panel_view ,dm.widthPixels,400,true);
        }else{
            _user_panel_windows = new PopupWindow(_user_panel_view,dm.widthPixels,App.dip2px(_activity,200),true);
        }
        _user_panel_windows.setAnimationStyle(R.style.Animation_UserPanel);
        
//        _user_panel_windows.setBackgroundDrawable(new BitmapDrawable());
        _user_panel_windows.setFocusable(true);
        
        //popWindow 响应返回键方法~~~
        _user_panel_view.setFocusable(true);
        _user_panel_view.setFocusableInTouchMode(true);
        _user_panel_view.requestFocus();
        _user_panel_view.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN 
						&& keyCode == KeyEvent.KEYCODE_BACK)
					_user_panel_windows.dismiss();

				return false;
			}
		});
        

        if(App.myAvatar!=null){
            _title_avatar_btn.setImageBitmap(App.myAvatar);
        }else{
            _title_avatar_btn.setImageBitmap(BitmapFactory.decodeResource(activity.getResources(),R.drawable.default_avatar));
        }

    }

    private View.OnClickListener avatarClickListener = new View.OnClickListener(){
        @Override
        public void onClick(View view) {
        	if(_user_panel_windows.isShowing()){
        		_user_panel_windows.dismiss();
        		return;
        	}

                 if(_activity.getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE){
                     _user_panel_windows.showAtLocation(_user_panel_view,Gravity.RIGHT,0,App.dip2px(_activity,60));
                 }else{
                     _user_panel_windows.showAtLocation(_user_panel_view, Gravity.TOP,0,App.dip2px(_activity,60));
                 }

            if(App.myAvatar!=null)
            _title_avatar_btn.setImageBitmap(App.myAvatar);



            _user_panel_view.findViewById(R.id.user_miniblog_lay).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
               _activity.startActivity(new Intent(_activity, MiniBlogActivity.class));
                _user_panel_windows.dismiss();
            }
            });

            //goto dou mail
            _user_panel_view.findViewById(R.id.user_mail).setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    _activity.startActivity(new Intent(_activity, MailActivity.class));
                    _user_panel_windows.dismiss();
                }
            });

            // goto user home page
            _user_panel_view.findViewById(R.id.user_index).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putString(Assistant.KEY_USER_ID, App.userId);
                    Intent indexIntent = new Intent(_activity, UserHomeActivity.class);
                    indexIntent.putExtras(bundle);
                    _activity.startActivity(indexIntent);
                    _user_panel_windows.dismiss();
                }
            });

            //goto user friends
            _user_panel_view.findViewById(R.id.user_friends).setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                       Bundle bundle = new Bundle();
                    bundle.putString(Assistant.KEY_USER_ID, App.userId);
                    bundle.putInt(Assistant.KEY_INDEX, Assistant.HOME_INDEX_CONTACTS);
                    Intent indexIntent = new Intent(_activity, UserHomeActivity.class);
                    indexIntent.putExtras(bundle);
                    _activity.startActivity(indexIntent);
                    _user_panel_windows.dismiss();
                }
            });

            //goto book
            _user_panel_view.findViewById(R.id.user_book).setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    _activity.startActivity(new Intent(_activity, CollectionBookListActivity.class));
                    _user_panel_windows.dismiss();
                }
            });

            //goto movie
            _user_panel_view.findViewById(R.id.user_movie).setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    _activity.startActivity(new Intent(_activity, CollectionMovieListActivity.class));
                    _user_panel_windows.dismiss();
                }
            });

            //goto music
            _user_panel_view.findViewById(R.id.user_music).setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    _activity.startActivity(new Intent(_activity, CollectionMusicListActivity.class));
                    _user_panel_windows.dismiss();
                }
            });

            //goto search
            _user_panel_view.findViewById(R.id.user_search).setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                   _user_panel_windows.dismiss();
                   PopupSearchBar popupSearchBar = new PopupSearchBar(_activity);
                   popupSearchBar.showSearchBar();
                }
            });
            
            //goto group
            _user_panel_view.findViewById(R.id.user_group).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
//					Toast.makeText(_activity, "小组功能开发ing,敬请期待", Toast.LENGTH_LONG).show();
					_activity.startActivity(new Intent(_activity, DouMyGroupAct.class));
					_user_panel_windows.dismiss();
				}
			});
        }
    };
    
    //显示左上角太阳图标.
    public void initTitleStyle(){
        format = new SimpleDateFormat("H");
        Date cure = new Date(System.currentTimeMillis());
        int hour = Integer.valueOf(format.format(cure));

        _title_sun_icon_image.setImageBitmap(BitmapFactory.decodeResource(_activity.getResources(),R.drawable.sun32));

        RelativeLayout.LayoutParams sun_params =
                (RelativeLayout.LayoutParams )_title_sun_icon_image.getLayoutParams();

        ImageView icon_view = new ImageView(_activity);
        icon_view.setId(icon_view_id);

        Bitmap bitmap_icon =  BitmapFactory.decodeResource(_activity.getResources(),R.drawable.sun32);
        Boolean isNight = false;

        if(hour>6&& hour<18){
            if(hour>12)
            hour -= 12;
        }else {
            return;
        }
        
        //无聊的太阳位置－！
        ImageView imageView = null;
        switch (hour){
            case 1:
               imageView = (ImageView)_activity.findViewById(R.id.title_sun_icon_image_1);
                imageView.setVisibility(View.VISIBLE);
                break;
            case 2:
                imageView = (ImageView)_activity.findViewById(R.id.title_sun_icon_image_2);
                imageView.setVisibility(View.VISIBLE);
                break;
            case 3:
                imageView = (ImageView)_activity.findViewById(R.id.title_sun_icon_image_3);
                imageView.setVisibility(View.VISIBLE);

                break;
            case 4:
                imageView = (ImageView)_activity.findViewById(R.id.title_sun_icon_image_4);
                        imageView.setVisibility(View.VISIBLE);
                break;
            case 5:
                imageView = (ImageView)_activity.findViewById(R.id.title_sun_icon_image_5);
                      imageView.setVisibility(View.VISIBLE);
                break;


            case 7:
                imageView = (ImageView)_activity.findViewById(R.id.title_sun_icon_image_7);
                        imageView.setVisibility(View.VISIBLE);
                break;
            case 8:
                imageView = (ImageView)_activity.findViewById(R.id.title_sun_icon_image_8);
                        imageView.setVisibility(View.VISIBLE);
                break;
            case 9:
                imageView = (ImageView)_activity.findViewById(R.id.title_sun_icon_image_9);
                        imageView.setVisibility(View.VISIBLE);
                break;
            case 10:
                imageView = (ImageView)_activity.findViewById(R.id.title_sun_icon_image_10);
                        imageView.setVisibility(View.VISIBLE);
                break;
            case 11:
                imageView = (ImageView)_activity.findViewById(R.id.title_sun_icon_image_11);
                        imageView.setVisibility(View.VISIBLE);
                break;
            case 12:
                imageView = (ImageView)_activity.findViewById(R.id.title_sun_icon_image_12);
                        imageView.setVisibility(View.VISIBLE);
                break;
            default:
//                _activity.findViewById(R.id.title_sun_icon_image_12).
//                        setVisibility(View.GONE);
                break;
        }

        //if(hour!=6)
        //imageView.setImageBitmap(bitmap_icon);

    }

    public void initPageController(int total,int current){
        if(total<2)
            return;

        _screen_count = total;
        _current_screen = current;

        int i=0;
        while (total>i){
            ImageView imageView = new ImageView(_activity);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(18,18);
            imageView.setLayoutParams(params);

            if(i==(current-1)){
                imageView.setImageBitmap(BitmapFactory.
                        decodeResource(_activity.getResources(),R.drawable.rect_green));
            }else{
                imageView.setImageBitmap(BitmapFactory.
                        decodeResource(_activity.getResources(),R.drawable.rect_grey));
            }

            _control_images.add(imageView);
            i++;
            _page_control_lay.addView(imageView);
        }
    }

    public void setCurrentPage(int current){
        if(current > _screen_count)
            return;

        for(ImageView imageView:_control_images){
            imageView.setImageBitmap(BitmapFactory.decodeResource(_activity.getResources(),R.drawable.rect_grey));
        }

        _control_images.get(current-1).setImageBitmap(
                BitmapFactory.decodeResource(_activity.getResources(),R.drawable.rect_green));

    }

    public void setTitle(String text){
        _title_tx.setText(text);
    }
    
    
    public interface WriteMiniBlog {
    	void writeMiniBlog();
    }
    
    //当为miniblog时 点击title为写miniblog
    public void initWriteMiniblog(final WriteMiniBlog write){
    	ImageButton sendMiniblog = (ImageButton)_activity.findViewById(R.id.titl_write_btn);
    	sendMiniblog.setVisibility(View.VISIBLE);
    	sendMiniblog.setOnClickListener(new OnClickListener(){
    		@Override
    		public void onClick(View v){
    			write.writeMiniBlog();
    		}
    	});
    }
    
    public void setAvatar(Bitmap bitmap){
        App.myAvatar = bitmap;
        _title_avatar_btn.setImageBitmap(App.myAvatar);
    }
    

}

